package util

import (
	"log"
	"os/exec"
	"strings"

	"github.com/codeskyblue/go-sh"
)

func Exec(str string) string {
	cmd := strings.Split(str, " ")
	var args []string
	if len(cmd) > 1 {
		args = cmd[1:]
	}
	log.Println("exec: ", str)
	f, err := exec.Command(cmd[0], args...).Output()
	if err != nil {
		log.Println("error: ", err.Error())
		return ""
	}
	log.Println("ret: ", string(f))
	return string(f)
}

func Command(str string) {
	cmd := strings.Split(str, " ")
	var args []interface{}
	if len(cmd) > 1 {
		args = make([]interface{}, len(cmd)-1)
		for i, v := range cmd[1:] {
			args[i] = v
		}
	}
	log.Println("command: ", str)
	sh.Command(cmd[0], args...).Run()
}
